package com.vtron.utils.io;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import java.io.*;
import java.util.List;

/**
 * @XML工具类
 * @date : 2021/12/23 16:11
 * @author: 忻龙彪
 */
public class XmlUtils {
    /**
     * @生成XML文件
     */
    public static void build(Document document, String fileName) throws Exception {
        File file = new File(fileName);
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }
        XMLOutputter out = new XMLOutputter();
        Format format = Format.getPrettyFormat();
        format.setTextMode(Format.TextMode.TRIM_FULL_WHITE);
        format.setIgnoreTrAXEscapingPIs(false);
        format.setEncoding("UTF-8");
        out.setFormat(format);
        out.output(document, new FileOutputStream(fileName));
    }

    public static Element getElement(String sname, String text) {
        Element element = new Element(sname);
        element.setText(text);
        return element;
    }
    // 读取XML文件
    public static Document load(String fileName) throws Exception {
        Document document = new SAXBuilder().build(fileName);
        return document;
    }
   /* private List<Element> getChildrenElement(Element root){
        List<Element> list = root.getChildren();

    }*/
}
